import { map } from "rxjs/operators";
import { PaginationResponse } from "@datorama/akita";

import {
  EditRoomParams,
  EtcDataReq,
  FileSyncsType,
  RoomStatReq,
  ShareListReq,
  SignInContentReq,
  SignInOption,
  SignInSettingReq,
} from "../models/request/roomReq";

import { File, LiveStream } from "../models/LiveRoom";
import {
  AudienceItem,
  EtcDataRsp,
  GetInvitesRsp,
  GetRoomRsp,
  HomePageListRsp,
  RoomItem
} from "../models/response/roomRsp";
import { rxRequest } from "@/utils/rxHelpers";
import CONFIG from "@/config";

export default {
  getRoomSvc(roomId: string) {
    return rxRequest<GetRoomRsp>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}`
    })
  },

  createRoomSvc(data: EditRoomParams) {
    return rxRequest<string>({
      url: CONFIG.BASE_URL + "/v1/live/room",
      // 需要授权访问
      withJwt: true,
      data,
      method: "POST"
    })
  },

  editRoomSvc(data: EditRoomParams) {
    return rxRequest<string>({
      url: CONFIG.BASE_URL + "/v1/live/room",
      // 需要授权访问
      withJwt: true,
      data,
      method: "PUT"
    })
  },

  //软删除房间
  delRoomSvc(roomId: string) {
    return rxRequest({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}`,
      // 需要授权访问
      withJwt: true,
      method: "DELETE"
    })
  },

  //获取房间文件
  getFilesSvc(roomId: string) {
    return rxRequest<File[]>({
      url: CONFIG.BASE_URL + `/v1//live/file/getFiles/${roomId}`
    })
  },

  //修改文件列表
  uploadFileSvc(roomId: string, fileName: string, fileUrl: string) {
    return rxRequest<File>({
      url: CONFIG.BASE_URL + "/v1/live/file/upload",
      // 需要授权访问
      withJwt: true,
      data: { roomId, fileName, fileUrl },
      method: "POST"
    })
  },

  //删除文件
  deleteFileSvc(roomId: string, fileId: string) {
    return rxRequest<File>({
      url: CONFIG.BASE_URL + "/v1/live/file/delete",
      // 需要授权访问
      withJwt: true,
      data: { roomId, fileId },
      method: "DELETE"
    })
  },

  //房间文件转码请求 
  fileTransCodeSvc(roomId: string, fileId: string, fileUrl: string) {
    return rxRequest({
      url: CONFIG.BASE_URL + "/v1/live/file/transCode",
      // 需要授权访问
      withJwt: true,
      data: { roomId, fileId, fileUrl },
      method: "POST"
    })
  },

  //发起文件演示同步
  fileSynSvc(roomId: string, fileId: string, transcodeUrl: string, page: number, pages: number) {
    return rxRequest({
      url: CONFIG.BASE_URL + "/v1/live/file/sync",
      // 需要授权访问
      withJwt: true,
      data: { roomId, fileId, transcodeUrl, page, pages },
      method: "POST"
    })
  },

  //播放回放时拉取文件同步记录
  getFileSyncsSvc(roomId: string) {
    return rxRequest<FileSyncsType[]>({
      url: CONFIG.BASE_URL + `/v1/live/file/getFileSyncs/${roomId}`,
      // 需要授权访问
      withJwt: true
    })
  },

  //主播加入缓存
  addAnchorSvc(roomId: string) {
    return rxRequest({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/anchor`,
      // 需要授权访问
      withJwt: true,
      method: "POST"
    })
  },

  //观众加入缓存
  addAudienceSvc(roomId: string) {
    return rxRequest({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/audience`,
      // 需要授权访问
      withJwt: true,
      method: "POST"
    })
  },
  //移除主播
  delAnchorSvc(roomId: string) {
    return rxRequest({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/anchor`,
      // 需要授权访问
      withJwt: true,
      method: "DELETE"
    })
  },

  delAudienceSvc(roomId: string) {
    return rxRequest({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/audience`,
      // 需要授权访问
      withJwt: true,
      method: "DELETE"
    })
  },

  delInviteAnchorSvc(roomId: string) {
    return rxRequest({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/delInviteAnchor`,
      // 需要授权访问
      withJwt: true,
      method: "DELETE"
    })
  },

  delAssistantSvc(roomId: string, userId: string) {
    const url = CONFIG.BASE_URL + `/v1/live/room/${roomId}/delAssistant/${userId}`;

    return rxRequest({
      url,
      // 需要授权访问
      withJwt: true,
      method: "DELETE"
    })
  },

  getLiveStreamsSvc(roomId: string) {
    return rxRequest<LiveStream[]>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/liveStreams`
    })
  },

  getLinkStreamsSvc(roomId: string) {
    return rxRequest<LiveStream[]>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/linkStreams`
    })
  },

  //获取点赞分享等数据
  getEtcDataSvc(roomId: string) {
    return rxRequest<EtcDataRsp>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/roomEtcData`
    })
  },

  //更新点赞分享等数据
  updateEtcDataSvc(roomId: string, data: EtcDataReq) {
    return rxRequest<EtcDataRsp>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/roomEtcData`,
      // 需要授权访问
      data,
      method: "PUT"
    })
  },

  //更新点赞分享等数据
  saveRoomStatSvc(roomId: string, data: RoomStatReq) {
    return rxRequest({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/saveRoomStats`,
      // 需要授权访问
      withJwt: true,
      data,
      method: "POST",
    })
  },

  //查询邀请数据
  getInvitesSvc(roomId: string) {
    return rxRequest<GetInvitesRsp>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/invites`,
      withJwt: true
    })
  },

  //主播接受邀请，需要调用notifyAnchorChange
  inviteAnchorSvc(roomId: string) {
    return rxRequest<GetInvitesRsp>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/inviteAnchor`,
      withJwt: true,
      method: "PUT"
    })
  },

  //助手接受邀请
  inviteAssistantSvc(roomId: string) {
    return rxRequest<GetInvitesRsp>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/inviteAssistant`,
      withJwt: true,
      method: "PUT"
    })
  },

  //开始推流的通知
  operateCameraSvc(roomId: string, operation: string) {
    return rxRequest({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/operate/${operation}`,
      withJwt: true,
      method: "PUT"
    })
  },

  getWatchedRoomSvc(pageNum: number, pageSize: number) {
    return rxRequest<PaginationResponse<RoomItem>>({
      url: CONFIG.BASE_URL + "/v1/live/room/watchedList",
      withJwt: true,
      data: { pageNum, pageSize },
      method: "POST"
    })
      .pipe(
        map(data => {
          if (data.data && data.data.length > 0) {
            data.data = data.data.map(item => {
              //把后端传回的日期字符串转成Date对象
              item.startTime = new Date(item.startTime);
              return item;
            })
          }
          return data;
        })
      )
  },

  getCreatedRoomSvc(pageNum: number, pageSize: number) {
    return rxRequest<PaginationResponse<RoomItem>>({
      url: CONFIG.BASE_URL + "/v1/live/room/myCreated",
      withJwt: true,
      data: { pageNum, pageSize },
      method: "POST"
    })
      .pipe(
        map(data => {
          if (data.data && data.data.length > 0) {
            data.data = data.data.map(item => {
              //把后端传回的日期字符串转成Date对象
              item.startTime = new Date(item.startTime);
              return item;
            })
          }
          return data;
        })
      )
  },

  getStartingRoomSvc(pageNum: number, pageSize: number) {
    return rxRequest<PaginationResponse<RoomItem>>({
      url: CONFIG.BASE_URL + "/v1/live/room/will/live",
      withJwt: true,
      data: { pageNum, pageSize },
      method: "POST",
      dataType: "json"
    })
      .pipe(
        map(data => {
          if (data.data && data.data.length > 0) {
            data.data = data.data.map(item => {
              //把后端传回的日期字符串转成Date对象
              item.startTime = new Date(item.startTime);
              return item;
            })
          }
          return data;
        })
      )
  },

  //更新用户心跳并获取点赞分享等数据
  updateTSSvc(roomId: string, userId: string) {
    return rxRequest<EtcDataRsp>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/updateTS/${userId}`,
      method: "PUT"
    })
  },

  //首页请求
  getHomePageSvc() {
    return rxRequest<HomePageListRsp>({
      url: CONFIG.BASE_URL + "/v1/live/room/getMain"
    })
      .pipe(
        map(data => {
          if (data.firstFloor && data.firstFloor.length > 0) {
            data.firstFloor = data.firstFloor.map(item => {
              //把后端传回的日期字符串转成Date对象
              item.startTime = new Date(item.startTime);
              return item;
            })
          }
          if (data.secondFloor && data.secondFloor.length > 0) {
            data.secondFloor = data.secondFloor.map(item => {
              //把后端传回的日期字符串转成Date对象
              item.startTime = new Date(item.startTime);
              return item;
            })
          }

          return data;
        })
      )
  },

  //签到设置
  signInSettingSvc(roomId: string, data: SignInSettingReq) {
    return rxRequest({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/signInSetting`,
      withJwt: true,
      data,
      method: "POST"
    })
  },

  // 获取签到信息
  getSignInFromSvc(roomId: string) {
    return rxRequest<SignInOption[]>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/signInFrom`,
      withJwt: true
    })
  },

  // 用户获取签到信息
  getUserSignInfoSvc(roomId: string) {
    return rxRequest<string>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/getUserSignInfo`,
      withJwt: true
    })
  },

  // 用户签到
  getUserSignInSvc(roomId: string, signInContent: SignInContentReq[]) {
    return rxRequest({
      url: CONFIG.BASE_URL + `/v1/live/room/userSignIn`,
      withJwt: true,
      data: { roomId, signInContent },
      method: "POST"
    })
  },

  //密码修改请求
  editRoomPwdSvc(data: EditRoomParams) {
    return rxRequest({
      url: CONFIG.BASE_URL + "/v1/live/room/pwd",
      // 需要授权访问
      withJwt: true,
      data,
      method: "PUT"
    })
  },

  // 获取分享榜refId
  getRefIdSvc(roomId: string) {
    return rxRequest<string>({
      url: CONFIG.BASE_URL + `/v1/live/room/getRoomReferral/${roomId}`,
      withJwt: true
    })
  },

  // 根据refId获取分享榜房间id
  getRoomByRefSvc(refId: string) {
    return rxRequest<string>({
      url: CONFIG.BASE_URL + `/v1/live/room/getRoomByRef/${refId}`,
      withJwt: true
    })
  },

  // 获取用户分享榜的信息
  getRefRankingSvc(roomId: string) {
    return rxRequest<ShareListReq[]>({
      url: CONFIG.BASE_URL + `/v1/live/room/refRanking/${roomId}`,
      withJwt: true
    })
  },

  // 获取连麦列表
  getSearchListSvc(roomId: string, pageNum: number, pageSize: number, searchKey?: string) {
    return rxRequest<PaginationResponse<AudienceItem>>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/searchAudience`,
      withJwt: true,
      data: { searchKey, pageNum, pageSize },
      method: "POST"
    })
  },

  // 小主播接受连麦
  accessAudienceInviteSvc(roomId: string) {
    return rxRequest<LiveStream>({
      url: CONFIG.BASE_URL + `/v1/live/room/${roomId}/acceptLinkMic`,
      withJwt: true,
      method: "POST"
    })
  },
}
